package com.pocketsweet.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.MyAlertDialog;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.wheelview.ScreenInfo;
import com.pocketsweet.ui.uilib.wheelview.WheelHeight;
import com.pocketsweet.ui.uilib.wheelview.WheelWeight;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_edit_look)
public class EditApperance extends BaseActivity implements OnClickListener {

	private HeaderLayout header;
	private MLUser mlUser = new MLUser();
	private String figure;
	WheelHeight wheelMain;
	WheelWeight wheelWeight;
	String height;
	String weight;
	boolean isRefresh = false;

	@ViewInject(R.id.iconFat)
	private static TextView iconFat;

	@ViewInject(R.id.tvHeight)
	private static TextView tvHeight;

	@ViewInject(R.id.tvWeight)
	private static TextView tvWeight;

	@ViewInject(R.id.iconBone)
	private static TextView iconBone;

	@ViewInject(R.id.iconSilm)
	private static TextView iconSilm;

	@ViewInject(R.id.iconBalance)
	private static TextView iconBalance;

	@ViewInject(R.id.iconRich)
	private static TextView iconRich;

	@ViewInject(R.id.iconPlentiful)
	private static TextView iconPlentiful;

	@ViewInject(R.id.iconStrong)
	private static TextView iconStrong;

	@ViewInject(R.id.tvFat)
	private static TextView tvFat;

	@ViewInject(R.id.tvBone)
	private static TextView tvBone;

	@ViewInject(R.id.tvSilm)
	private static TextView tvSilm;

	@ViewInject(R.id.tvBalance)
	private static TextView tvBalance;

	@ViewInject(R.id.tvRich)
	private static TextView tvRich;

	@ViewInject(R.id.tvPlentiful)
	private static TextView tvPlentiful;

	@ViewInject(R.id.tvStrong)
	private static TextView tvStrong;

	@ViewInject(R.id.relFat)
	private static RelativeLayout relFat;

	@ViewInject(R.id.relBone)
	private static RelativeLayout relBone;

	@ViewInject(R.id.relSilm)
	private static RelativeLayout relSilm;

	@ViewInject(R.id.relBalance)
	private static RelativeLayout relBalance;

	@ViewInject(R.id.relRich)
	private static RelativeLayout relRich;

	@ViewInject(R.id.relPlentiful)
	private static RelativeLayout relPlentiful;

	@ViewInject(R.id.relStrong)
	private static RelativeLayout relStrong;

	@ViewInject(R.id.relHeight)
	private static RelativeLayout relHeight;

	@ViewInject(R.id.relWeight)
	private static RelativeLayout relWeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		initView();
		setHeaderFunction();
		if (!StringUtils.isEmpty(String.valueOf(mlUser.getWeight()))
				&& !String.valueOf(mlUser.getWeight()).equals("0")) {
			tvWeight.setText(String.valueOf(mlUser.getWeight()) + "kg");
		}
		if (!StringUtils.isEmpty(String.valueOf(mlUser.getHeight()))
				&& !String.valueOf(mlUser.getHeight()).equals("0")) {
			tvHeight.setText(String.valueOf(mlUser.getHeight()) + "cm");
		}
	}

	private void initView() {
		relFat.setOnClickListener(this);
		relBone.setOnClickListener(this);
		relSilm.setOnClickListener(this);
		relBalance.setOnClickListener(this);
		relRich.setOnClickListener(this);
		relPlentiful.setOnClickListener(this);
		relStrong.setOnClickListener(this);
		relHeight.setOnClickListener(this);
		relWeight.setOnClickListener(this);
		figure = getIntent().getStringExtra("figure");
		initSelect(figure);
	}

	private void initSelect(String figure) {
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("圆球状")) {
				iconFat.setVisibility(View.VISIBLE);
				tvFat.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("骨感")) {
				iconBone.setVisibility(View.VISIBLE);
				tvBone.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("苗条")) {
				iconSilm.setVisibility(View.VISIBLE);
				tvSilm.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("匀称")) {
				iconBalance.setVisibility(View.VISIBLE);
				tvBalance.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("富态")) {
				iconRich.setVisibility(View.VISIBLE);
				tvRich.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("丰满")) {
				iconPlentiful.setVisibility(View.VISIBLE);
				tvPlentiful.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
		if (!StringUtils.isEmpty(figure)) {
			if (figure.equals("健壮")) {
				iconStrong.setVisibility(View.VISIBLE);
				tvStrong.setTextColor(getResources().getColor(
						R.color.main_text_color_border));
				return;
			}
		}
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("");
		header.setMiddleText("外貌");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				if (isRefresh) {
					mlUser.saveInBackground(new SaveCallback() {

						@Override
						public void done(AVException e) {
							if (e == null) {
								ToolKits.toast(EditApperance.this, "修改成功");
							} else {
								ToolKits.toast(EditApperance.this, "请检查网络");
							}
						}
					});
				}
				EditApperance.this.finish();
			}
		});
	}

	private int calculatefigure(String figure) {
		int f = Integer.valueOf(figure);
		return f;
	}

	private void initFigure() {
		iconFat.setVisibility(View.GONE);
		iconBone.setVisibility(View.GONE);
		iconSilm.setVisibility(View.GONE);
		iconBalance.setVisibility(View.GONE);
		iconRich.setVisibility(View.GONE);
		iconPlentiful.setVisibility(View.GONE);
		iconStrong.setVisibility(View.GONE);
		tvFat.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvBone.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvSilm.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvBalance.setTextColor(getResources().getColor(
				R.color.profile_text_edit));
		tvRich.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvPlentiful.setTextColor(getResources().getColor(
				R.color.profile_text_edit));
		tvStrong.setTextColor(getResources()
				.getColor(R.color.profile_text_edit));
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.relHeight: // 身高更改弹出框
			LayoutInflater inflater1 = LayoutInflater.from(EditApperance.this);
			final View timepickerview1 = inflater1.inflate(R.layout.timepicker,
					null);
			ScreenInfo screenInfo1 = new ScreenInfo(EditApperance.this);
			wheelMain = new WheelHeight(timepickerview1);
			wheelMain.screenheight = screenInfo1.getHeight();
			wheelMain.initDateTimePicker();
			final MyAlertDialog dialog = new MyAlertDialog(EditApperance.this)
					.builder().setTitle("身高").setView(timepickerview1)
					.setNegativeButton("取消", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					});
			dialog.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(View v) {
					height = wheelMain.getTime();
					if (String.valueOf(calculatefigure(height)).equals("0")) {
						tvHeight.setText("");
					} else {
						tvHeight.setText(String
								.valueOf(calculatefigure(height)) + "cm");
					}
					mlUser.setHeight(calculatefigure(height));
					isRefresh = true;
				}
			});
			dialog.show();
			break;
		case R.id.relWeight: // 体重更改弹出框
			LayoutInflater inflater2 = LayoutInflater.from(EditApperance.this);
			final View timepickerview2 = inflater2.inflate(R.layout.timepicker,
					null);
			ScreenInfo screenInfo2 = new ScreenInfo(EditApperance.this);
			wheelWeight = new WheelWeight(timepickerview2);
			wheelWeight.screenheight = screenInfo2.getHeight();
			wheelWeight.initDateTimePicker();
			final MyAlertDialog dialog2 = new MyAlertDialog(EditApperance.this)
					.builder().setTitle("体重").setView(timepickerview2)
					.setNegativeButton("取消", new OnClickListener() {
						@Override
						public void onClick(View v) {

						}
					});
			dialog2.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(View v) {
					weight = wheelWeight.getTime();
					if (String.valueOf(calculatefigure(weight)).equals("0")) {
						tvWeight.setText("");
					} else {
						tvWeight.setText(String
								.valueOf(calculatefigure(weight)) + "kg");
					}
					mlUser.setWeight(calculatefigure(weight));
					isRefresh = true;
				}
			});
			dialog2.show();

			break;
		case R.id.relFat:
			initFigure();
			mlUser.setFigure("圆球状");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relBone:
			initFigure();
			mlUser.setFigure("骨感");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relSilm:
			initFigure();
			mlUser.setFigure("苗条");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relBalance:
			initFigure();
			mlUser.setFigure("匀称");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relRich:
			initFigure();
			mlUser.setFigure("富态");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relPlentiful:
			initFigure();
			mlUser.setFigure("丰满");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		case R.id.relStrong:
			initFigure();
			mlUser.setFigure("健壮");
			isRefresh = true;
			initSelect(mlUser.getFigure());
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// 此处写处理的事件
			mlUser.saveInBackground(new SaveCallback() {
				@Override
				public void done(AVException arg0) {
					if (arg0 == null) {
						ToolKits.toast(EditApperance.this, "修改成功");
					}
				}
			});
			EditApperance.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

}
